import 'package:base_lib/common/index.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/routers.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:doctor_prescribe_lib/offline/basic_info/bloc.dart';
import 'package:doctor_prescribe_lib/prescribe_util.dart';
import 'package:doctor_prescribe_lib/repository/http.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../western_medicine/medical_search/entity/medical_search_entity.dart'
    as MedicalSearchBean;

class ChineseMedicineSearchPage extends StatefulWidget {
  final Parameters parameters;

  ChineseMedicineSearchPage(this.parameters);

  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends BaseThemeState<ChineseMedicineSearchPage> {
  int orgTypeID;
  String orgID;

  List<MedicalSearchBean.Datas> list;

  @override
  void initState() {
    super.initState();
    orgTypeID = widget.parameters.getInt("orgTypeID");
    orgID = widget.parameters.getString("orgID");
    doSearch();
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text('药品搜索'),
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.all(10),
              child: TextField(
                onSubmitted: (str) {
                  doSearch(str: str);
                },
                textInputAction: TextInputAction.search,
                style: TextStyle(fontSize: 16),
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(left: 10),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10)),
                    hintText: '请输入药品名称或药品简码',
                    hintStyle: TextStyle(fontSize: 12)),
              ),
            ),
            Expanded(
              child: list == null
                  ? buildLoadingIndicator()
                  : list.isNotEmpty
                      ? ListView.builder(
                          itemCount: list.length,
                          itemBuilder: (context, pos) {
                            var item = list[pos];
                            return Container(
                              child: Row(
                                children: [
                                  10.hGapDp,
                                  Expanded(
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        10.vGapDp,
                                        Text(
                                          item.medicineName,
                                          style: TextStyle(
                                              fontSize: 18,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold),
                                        ),
                                        10.vGapDp,
                                        Text(
                                          '${PrescribeUtil.formatMoney(item.unitPrice)}元/g',
                                          style: TextStyle(
                                              fontSize: 12, color: Colors.red),
                                        ),
                                        10.vGapDp,
                                        1.gapV(Colors.grey[300]),
                                      ],
                                    ),
                                  ),
                                  IconButton(
                                    onPressed: () {
                                      Routers.navigateTo(
                                              context,
                                              DoctorPrescribeComponent
                                                  .chineseDosagePage,
                                              parameters: Parameters()
                                                ..putObj('item', item))
                                          .then((value) {
                                        if (value == true) {
                                          Routers.pop(context, true);
                                        }
                                      });
                                    },
                                    icon: Icon(
                                      Icons.add_circle_outline,
                                      color: theme.themeColor.themeColor,
                                    ),
                                  )
                                ],
                              ),
                            );
                          })
                      : Center(
                          child: Text('很抱歉，未搜索到相关药品~'),
                        ),
            ),
          ],
        ),
      ),
    );
  }

  void doSearch({String str}) async {
    var result = await PrescribeHttp.searchChineseMedicine(str ?? '', orgTypeID,
        orgID, OfflineBasicInfoBloc.basicInfoData.medicineFormID);

    if (result.isSuccess) {
      list = result.data;
      doRefresh();
    } else {
      ToastUtil.error(result.error.msg);
    }
  }

  void doRefresh() {
    setState(() {});
  }
}


